package com.yl.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yl.auth.mapper.LaorenMapper;
import com.yl.auth.model.entity.Chuangwei;
import com.yl.auth.entity.Fangjian;
import com.yl.auth.mapper.ChuangweiMapper;
import com.yl.auth.mapper.FangjianMapper;
import com.yl.auth.service.FangjianService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ln
 * @since 2023-06-16
 */
@Service
public class FangjianServiceImpl extends ServiceImpl<FangjianMapper, Fangjian> implements FangjianService {
    @Resource
    private FangjianMapper fangjianMapper;
    @Resource
    private ChuangweiMapper chuangweiMapper;
    @Resource
    private LaorenMapper laorenMapper;
    @Override
    public Fangjian selectById(Long id) {
        Fangjian fangjian=fangjianMapper.selectById(id);
        Integer count=0;
        if(fangjian.getId()!=null){
            QueryWrapper<Chuangwei> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("room_id",fangjian.getId());
            List<Chuangwei> chuangweis = chuangweiMapper.selectList(queryWrapper);
            for (Chuangwei chuangwei : chuangweis) {
                // 检查当前床位所在的房间是否有老人
                if (chuangwei.getRoomId() != null) {
                    count++;
                }
            }
        }
        fangjian.setCount(count);
        return fangjian;
    }
}
